package nsu.edu.zsq.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.SchClass;
import nsu.edu.zsq.bean.SchMajor;
import nsu.edu.zsq.bean.StuScore;
import nsu.edu.zsq.bean.Student;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.param.StuScoreParam;
import nsu.edu.zsq.service.sys.CourseService;
import nsu.edu.zsq.service.sys.SchClassService;
import nsu.edu.zsq.service.sys.SchMajorService;
import nsu.edu.zsq.service.sys.StuScoreService;

/** 
* Description: 学生成绩的controller类<br>
*/
@RequestMapping("/sys/student/score")
@Controller
public class StudentScoreController {
    
    @Autowired
    private CourseService courseService;
    @Autowired
    private SchMajorService schMajorService;
    @Autowired
    private SchClassService schClassService;
    @Autowired
    private StuScoreService stuScoreService;
    
    /**
     * 获取院系+专业的tree结构数据
     * @return JsonData
     */
    @RequestMapping("/schOrgWithMajorTree")
    @ResponseBody
    public JsonData schOrgWithMajorTree() {
        String listSchOrgWithMajorTree = courseService.listSchOrgWithMajorTree();
        List<SchMajor> listSchMajor = schMajorService.listSchMajor();
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("listSchOrgWithMajorTree", listSchOrgWithMajorTree);
        map.put("listSchMajor", listSchMajor);
        return JsonData.success(map);
    }
    
    /**
     * 根据专业代码获取班级的分页数据
     * @param majorNo 专业代码
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listSchClass")
    @ResponseBody
    public JsonData listSchClassWithPage(@RequestParam("majorNo") String majorNo, PageQuery pageQuery) {
        PageInfo<SchClass> listSch = schClassService.listSchClassWithPageByMajorNo(majorNo, pageQuery);
        return JsonData.success(listSch);
    }
    
    /**
     * 跳转到学生列表页面
     * @param classId 班级id
     * @param model
     * @return String manager/student/score/stuClass_student_list.html
     */
    @RequestMapping("/tostuClass_student_list")
    public String tostuClass_student_list(@RequestParam("classId") int classId, Model model) {
        model.addAttribute("classId", classId);
        return "manager/student/score/stuClass_student_list.html";
    }
    
    /**
     * 获取该班级的所有学生
     * @param classId 班级id
     * @param pageQuery 分页信息
     * @return JsonData
     */
    @RequestMapping("/listStudent")
    @ResponseBody
    public JsonData listStudent(@RequestParam("classId") int classId, PageQuery pageQuery) {
        PageInfo<Student> listStudent = stuScoreService.listStudent(classId, pageQuery);
        return JsonData.success(listStudent);
    }
    
    /**
     * 跳转到成绩管理页面
     * @param stuId 学生id
     * @param stuNo 学号
     * @param model
     * @return String manager/student/score/stuCourse_list.html
     */
    @RequestMapping("/toStuCourse_list")
    public String toStuCourse_list(@RequestParam("stuId") int stuId,@RequestParam("stuNo") String stuNo, Model model) {
        model.addAttribute("stuId", stuId);
        model.addAttribute("stuNo", stuNo);
        return "manager/student/score/stuCourse_list.html";
    }
    
    /**
     * 获取该学生所有的课程成绩（必修+选修）
     * @param stuId 学生id
     * @return JsonData
     */
    @RequestMapping("/listStuScore")
    @ResponseBody
    public JsonData listStuScore(@RequestParam("stuId") int stuId) {
        List<StuScoreParam> listStuScore = stuScoreService.listStuScore(stuId);
        return JsonData.success(listStuScore);
    }
    
    /**
     * 保存/编辑学生课程成绩信息
     * @param stuScore 学生课程成绩
     * @return JsonData
     */
    @RequestMapping("/saveStuScore")
    @ResponseBody
    public JsonData saveStuScore(StuScore stuScore) {
        stuScoreService.saveStuScore(stuScore);
        return JsonData.success();
    }
    
    // ==================================补考成绩管理===================================
    
    /**
     * 跳转到补考学生页面
     * @param classId 班级id
     * @param model
     * @return String manager/student/score/resit_stuClass_student_list.html
     */
    @RequestMapping("/toResit_stuClass_student_list")
    public String toResit_stuClass_student_list(@RequestParam("classId") int classId, Model model) {
        model.addAttribute("classId", classId);
        return "manager/student/score/resit_stuClass_student_list.html";
    }
    
    /**
     * 获取该班级所有需要补考的学生
     * @param classId 班级id
     * @param pageQuery 分页信息
     * @return JsonData
     */
    @RequestMapping("/listResitStudent")
    @ResponseBody
    public JsonData listResitStudent(@RequestParam("classId") int classId, PageQuery pageQuery) {
        PageInfo<Student> listStudent = stuScoreService.listResitStudent(classId, pageQuery);
        return JsonData.success(listStudent);
    }
    
    /**
     * 跳转到补考成绩管理页面
     * @param stuNo 学号
     * @param model
     * @return String manager/student/score/resit_stuCourse_list.html
     */
    @RequestMapping("/toResit_stuCourse_list")
    public String toResit_stuCourse_list(@RequestParam("stuNo") String stuNo, Model model) {
        model.addAttribute("stuNo", stuNo);
        return "manager/student/score/resit_stuCourse_list.html";
    }
    
    /**
     * 获取该学生所有的补考课程成绩（必修+选修）
     * @param stuId 学生id
     * @return JsonData
     */
    @RequestMapping("/listStuResitScore")
    @ResponseBody
    public JsonData listStuResitScore(@RequestParam("stuNo") String stuNo) {
        List<StuScore> listStuScore = stuScoreService.listStuResitScore(stuNo);
        return JsonData.success(listStuScore);
    }
    
    /**
     * 保存/编辑学生补考成绩信息
     * @param stuScore 学生课程成绩
     * @return JsonData
     */
    @RequestMapping("/saveStuResitScore")
    @ResponseBody
    public JsonData saveStuResitScore(StuScore stuScore) {
        stuScoreService.saveStuResitScore(stuScore);
        return JsonData.success();
    }
    
    // ==================================重修成绩管理===================================
    
    /**
     * 跳转到重修学生页面
     * @param classId 班级id
     * @param model
     * @return String manager/student/score/retake_stuClass_student_list.html
     */
    @RequestMapping("/toRetake_stuClass_student_list")
    public String toRetake_stuClass_student_list(@RequestParam("classId") int classId, Model model) {
        model.addAttribute("classId", classId);
        return "manager/student/score/retake_stuClass_student_list.html";
    }
    
    /**
     * 获取该班级所有需要补考的学生
     * @param classId 班级id
     * @param pageQuery 分页信息
     * @return JsonData
     */
    @RequestMapping("/listRetakeStudent")
    @ResponseBody
    public JsonData listRetakeStudent(@RequestParam("classId") int classId, PageQuery pageQuery) {
        PageInfo<Student> listStudent = stuScoreService.listRetakeStudent(classId, pageQuery);
        return JsonData.success(listStudent);
    }
    
    /**
     * 跳转到重修成绩管理页面
     * @param stuNo 学号
     * @param model
     * @return String manager/student/score/retake_stuCourse_list.html
     */
    @RequestMapping("/toRetake_stuCourse_list")
    public String toRetake_stuCourse_list(@RequestParam("stuNo") String stuNo, Model model) {
        model.addAttribute("stuNo", stuNo);
        return "manager/student/score/retake_stuCourse_list.html";
    }
    
    /**
     * 获取该学生所有的重修课程成绩（必修+选修）
     * @param stuId 学生id
     * @return JsonData
     */
    @RequestMapping("/listStuRetakeScore")
    @ResponseBody
    public JsonData listStuRetakeScore(@RequestParam("stuNo") String stuNo) {
        List<StuScore> listStuScore = stuScoreService.listStuRetakeScore(stuNo);
        return JsonData.success(listStuScore);
    }
    
    /**
     * 保存/编辑学生重修成绩信息
     * @param stuScore 学生课程成绩
     * @return JsonData
     */
    @RequestMapping("/saveStuRetakeScore")
    @ResponseBody
    public JsonData saveStuRetakeScore(StuScore stuScore) {
        stuScoreService.saveStuRetakeScore(stuScore);
        return JsonData.success();
    }
}
